﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Enemy_BOSS_Controller : MonoBehaviour
{
    Animator am;
    float distance;
    float attackTime;
    int jumpCount;
    float lastAttackTime;
    float violentTime;
    bool isMove;

    public Transform player;
    public Text score;
    public Transform weapon;
    public Transform hpUI;
    public int bossScore;
    public float atk_Distance;
    public int maxHp;
    public int hp;
    public float speed = 14;
    public float jump_Power = 40;
    public int atk;


    void Start()
    {
        am = GetComponent<Animator>();
        foreach (var item in am.runtimeAnimatorController.animationClips)
        {
            if (item.name.Equals("EnemyC_ATK_A"))
            {
                AnimationEvent ae = new AnimationEvent();
                ae.time = item.length - 0.2F;
                ae.functionName = "Fire";
                item.AddEvent(ae);
            }
        }
    }

    void Update()
    {

        if (isMove && player != null)
        {
            // 检测血量
            if (hp <= 0)
            {
                hpUI.gameObject.SetActive(false);
                Destroy(gameObject);
                // 游戏加分
                score.text = "Score  " + (int.Parse(score.text.Split(' ')[2]) + bossScore).ToString().PadLeft(6, '0');
            }

            Move();
            Attack();
            Violent();
        }
        else
        {
            am.SetTrigger("Idle");
        }
    }


    void Jump()
    {
        if (Physics2D.Raycast(transform.position, Vector2.down, 0.5F))
        {
            jumpCount = 0;
        }
        if (jumpCount < 2)
        {
            if (player.position.y - transform.position.y > 1)
            {
                jumpCount++;
                gameObject.GetComponent<Rigidbody2D>().velocity = Vector2.up * jump_Power;
            }
        }
    }

    void Move()
    {
        if (player != null)
        {
            distance = player.position.x - transform.position.x;

            am.SetTrigger("Run");

            // 面朝方向
            if (distance >= 0)
            {
                transform.localScale = new Vector3(Mathf.Abs(transform.localScale.x), transform.localScale.y, transform.localScale.z);
            }
            else
            {
                transform.localScale = new Vector3(Mathf.Abs(transform.localScale.x) * -1, transform.localScale.y, transform.localScale.z);
            }

            // 移动方向
            if (distance >= atk_Distance)
            {
                transform.Translate(Vector2.left * -speed * Time.deltaTime);
                Jump();
            }
            if (distance <= -atk_Distance)
            {
                transform.Translate(Vector2.left * speed * Time.deltaTime);
                Jump();
            }
        }
    }

    void Attack()
    {
        if (Time.time - lastAttackTime >= 1.5F)
        {
            if (distance <= atk_Distance && distance >= 0 || distance >= -atk_Distance && distance <= 0)
            {
                am.ResetTrigger("Run");
                am.SetTrigger("Attack_A");
                lastAttackTime = Time.time;

            }
            attackTime += Time.deltaTime;
            if (attackTime >= 1.0F)
            {
                attackTime = 0;
            }
        }
    }

    void Fire()
    {
        Transform darts = Instantiate(weapon, new Vector3(transform.position.x, transform.position.y + 1.5F, transform.position.z), Quaternion.identity);
        if (player != null)
        {
            if (distance >= 0)
            {
                darts.GetComponent<Rigidbody2D>().velocity = -Vector2.left * player.GetComponent<PlayerController>().speed;
            }
            else
            {
                darts.GetComponent<Rigidbody2D>().velocity = Vector2.left * player.GetComponent<PlayerController>().speed;
            }
        }

        Destroy(darts.gameObject, 2);

    }

    void Violent()
    {
        if (Time.time - violentTime >= 3)
        {
            if (hp <= maxHp / 2)
            {
                violentTime = Time.time;
                Attack();
                for (int i = 1; i < 11; i++)
                {
                    Transform darts = Instantiate(weapon, new Vector3(transform.position.x, transform.position.y + 1.5F, transform.position.z), Quaternion.Euler(0, 0, 36 * i));
                    Destroy(darts.gameObject, 2);
                    darts.GetComponent<Rigidbody2D>().velocity = darts.up * player.GetComponent<PlayerController>().speed;
                }
            }
        }
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.tag.Equals("PlayerBody"))
        {
            isMove = true;
            transform.FindChild("IsMove").gameObject.SetActive(false);
            hpUI.gameObject.SetActive(true);
        }
    }

}
